/**
* ...
* @author Ciprian Caba
* @contact caba.ciprian@gmail.com
* @version 0.1
*/

package com.cipriancaba.loaders {

	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.*;
	
	import com.cipriancaba.events.XMLEvent;


	public class XMLLoader extends EventDispatcher {
		
		private var _xmlLoader:URLLoader;
		
		public function XMLLoader(url:String) {
			var XML_Request:URLRequest = new URLRequest(url);
			_xmlLoader = new URLLoader(XML_Request);
			addEventListeners();
		}
		
		private function addEventListeners():void {
			_xmlLoader.addEventListener(Event.COMPLETE, completeHandler);
            _xmlLoader.addEventListener(Event.OPEN, openHandler);
            _xmlLoader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            _xmlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            _xmlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            _xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		
		private function completeHandler(event:Event):void {
            dispatchEvent(new XMLEvent("loadComplete", XML(_xmlLoader.data)));
        }

        private function openHandler(event:Event):void {
            //trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            //trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            //trace("securityErrorHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
            //trace("httpStatusHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            //trace("ioErrorHandler: " + event);
        }
	}
}
